package com.aaa.dang.modules.repairs.service.Impl;

import com.aaa.dang.common.entity.Repair;
import com.aaa.dang.common.util.ResultModel;
import com.aaa.dang.modules.repairs.dao.IRpairsDao;
import com.aaa.dang.modules.repairs.service.IRepairsService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.Map;

import static com.aaa.dang.common.systemconst.ISystemConst.FAULT_CODE;
import static com.aaa.dang.common.systemconst.ISystemConst.SUCCESS_CODE;

/**
 * @Author: ymk
 * @Date: 2018/12/14 14:52
 * @description:报修业务实现
 */
@Service
public class RepairsServiceImpl implements IRepairsService {
    @Autowired
    private IRpairsDao dao;
    @Autowired
    private ResultModel model;

    /**
     * 查询所有未处理完成的订单、分页、模糊查询
     * @param query
     * @return
     */
    @Override
    public ResultModel getAll(Map<String,Object> query) {
        int pageNo =Integer.parseInt(query.get("page")+"");
        int pageSize =Integer.parseInt(query.get("limit")+"");
        PageHelper.startPage(pageNo,pageSize);
        try {
            model.setData(dao.getAll(query));
            model.setCount(dao.getTotal(query));
            model.setCode(SUCCESS_CODE);
            model.setMsg("查询成功");

        }catch (Exception e){
            model.setCode(FAULT_CODE);
            model.setMsg("查询失败");
        }
        finally {
            return model;
        }



    }

    /**
     * 根据id查询详情
     * @param id
     * @return
     */
    @Override
    public Repair queryById(int id) {

        return dao.queryById(id);
    }

    /**
     * 更新申请单状态
     * @param id
     * @param status
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    @Override
    public ResultModel updateState(int id, int status) {
        try{
            int i = dao.updateState(id,status);
            if (i > 0) {
                model.setMsg("更新成功");
            } else {
                model.setMsg("更新失败");
            }

        }catch (Exception e){
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }

        return model;
    }

}
